import bpy
import sys

def export_fbx(fbx_path):
    bpy.ops.export_scene.fbx( 
                filepath=fbx_path,
        
                use_selection=True,            # True=仅导出选中物体；False=全部可见 
                use_active_collection=False,
        
                global_scale=1.0,
                apply_unit_scale=True,
                apply_scale_options='FBX_SCALE_UNITS',
        
                use_mesh_modifiers=True,
                mesh_smooth_type='FACE',         # 'FACE'/'EDGE'/'OFF'
        
                bake_space_transform=False,
                axis_forward='-Z',
                axis_up='Y',
        
                use_mesh_edges=False,
                use_tspace=True,
        
                use_custom_props=False,
        
                add_leaf_bones=False,
                primary_bone_axis='Y',
                secondary_bone_axis='X',
                armature_nodetype='NULL',
                bake_anim=False,
                bake_anim_use_all_actions=False,
        
                path_mode='COPY',               # COPY=把纹理拷到输出目录 
                embed_textures=True             # True=内嵌纹理；False=仅引用拷贝 
            )
            
    print("FBX exported to", fbx_path)

def export_obj(obj_path):
    bpy.ops.wm.obj_export( 
                filepath=str(obj_path),
                export_selected_objects=True,
                export_materials=True,
                path_mode="AUTO",
                forward_axis="NEGATIVE_Z",
                up_axis="Y"
            )
    print("OBJ exported to", obj_path)

def export_model(model_path):
    model_type = model_path[-3:].lower()
    if model_type == 'fbx':
        export_fbx(model_path)
    elif model_type == 'obj':
        export_obj(model_path)
    else:
        print(f"只支持导出fbx和obj, 您输入的是：{model_path}")

def parse_cli_args():
    argv = sys.argv[sys.argv.index("--")+1:]  if "--" in sys.argv  else []
    args_map = {}
    k = None
    for a in argv:
        if a.startswith('-'):
            if k:
                args_map[k] = 'no value'
            k = a[1:]
        else:
            if k:
                args_map[k] = a
                k = None
    return args_map

def delete_default_cube():
    cube = bpy.data.objects.get("Cube") 
    if cube:
        # 清除所有关联数据（对象、网格、材质等）
        bpy.data.objects.remove(cube,  do_unlink=True)
        mesh = bpy.data.meshes.get("Cube") 
        if mesh: bpy.data.meshes.remove(mesh) 

def delete_default_objs():
    default_objs = ['Cube', 'Light', 'Camera']
    for o_name in default_objs:
        obj = bpy.data.objects.get(o_name) 
        if obj:
            # 清除所有关联数据（对象、网格、材质等）
            bpy.data.objects.remove(obj,  do_unlink=True)
            mesh = bpy.data.meshes.get(o_name) 
            if mesh: bpy.data.meshes.remove(mesh) 